package com.dbManage.page.login;

import com.dbManage.common.Constant;
import com.dbManage.dao.UserDao;
import com.dbManage.entity.User;
import com.dbManage.exception.CheckCodeException;
import com.dbManage.exception.UsernameException;
import com.dbManage.tools.CodeTool;
import com.dbManage.tools.SessionTool;
import com.wsse.common.util.TimeUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@Repository
public class LoginService {
    @Resource
    UserDao userDao;

    /**
     * 登录验证
     *
     * @param user    登录账户
     * @param checkCode
     */
    public void loginCheck( User user, String checkCode) throws CheckCodeException, UsernameException {
//        String code = (String) SessionTool.getSession().getAttribute("checkCode");
//        if (code==null) {
//            throw new CheckCodeException("请重新获取验证码");
//        }
//        if (!code.equals(checkCode.toLowerCase())) {
//            throw new CheckCodeException("验证码错误");
//        }
        //账号对应信息
        User userInfo = userDao.findUser(user.getUsername());
        if (userInfo==null)
            throw new UsernameException("账号不存在");
        if(!userInfo.getPassword().equals(user.getPassword()))
            throw new UsernameException("密码错误");
        SessionTool.setUser(userInfo);
    }

    /**
     * 注册
     *
     * @param user
     * @param checkCode
     */
    public void register(User user, String checkCode) throws CheckCodeException, UsernameException {
        String code = (String) SessionTool.getSession().getAttribute("checkCode");
        if (code==null) {
            throw new CheckCodeException("请重新获取验证码");
        }
        if (!code.equals(checkCode.toLowerCase())) {
            throw new CheckCodeException("验证码错误");
        }
        user.setTime(TimeUtil.getNow());
        user.setRole(Constant.ROLE_GENERAL);
        try {
            userDao.insertUser(user);
            SessionTool.setUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UsernameException("该账号已注册");
        }

    }

    /**
     * 修改密码
     *
     * @param session
     * @param id
     * @param password
     */
    public void changePassword(HttpSession session, int id, String password) {
        User user = SessionTool.getUser();
//        用户自己修改密码或管理员重置密码
        if (user != null && (id == user.getId() || user.getRole() == 1)) {
            user.setId(id);
            user.setPassword(password);
            userDao.updatePassword(user);
        } else {
            throw new RuntimeException();
        }
    }

    /**
     * 获取验证码图片
     */
    public BufferedImage getImage(HttpSession session) {
        String codeStr = CodeTool.getNumber(6);
        BufferedImage image = CodeTool.getImage(codeStr);
        if (image == null) {
            throw new RuntimeException();
        } else {
            session.setAttribute("checkCode", codeStr.toLowerCase());
            return image;
        }
    }


}
